﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.astd.common.manager;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class RenrenLogin : LoginImplBase
    {
        public RenrenLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string encode_username = Uri.EscapeDataString(_acc.UserName);
            string encode_password = Uri.EscapeDataString(_acc.Password);
            HttpResult resp;

            string pre_cookie_url = "http://www.renren.com/";
            resp = TransferMgr.doGetPure(pre_cookie_url, ref cookies);

            string loginurl = String.Format(
                "http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201400150336",
                TimeMgr.TimeStamp, encode_username, _acc.Password);
            string data = String.Format(
                "email={0}&password={1}&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login&f=",
                encode_username, encode_password);
            if (verify_code != null && verify_code != "")
            {
                data = String.Format(
                "email={0}&password={1}&icode={2}&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login&f=",
                encode_username, encode_password, verify_code);
            }
            resp = TransferMgr.doPostPure(loginurl, data, ref cookies, "http://www.renren.com/ajaxproxy.htm");
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();
            //{"catchaCount":1,"code":false,
            //      "homeUrl":"http://www.renren.com/SysHome.do?origURL=http%3A%2F%2Fwww.renren.com%2Fhome&catchaCount=1&failCode=128",
            //      "failDescription":"您的用户名和密码不匹配","failCode":128,"email":"doomsword"}
            //{"catchaCount":4,"code":false,
            //      "homeUrl":"http://www.renren.com/SysHome.do?origURL=http%3A%2F%2Fwww.renren.com%2Fhome&catchaCount=4&failCode=512",
            //      "failDescription":"您输入的验证码不正确","failCode":512,"email":"doomsword"}
            if (retstr.IndexOf("您输入的验证码不正确") >= 0)
            {
                result.StatusCode = LoginStatusCode.NeedVerifyCode;
                result.CaptchaUrl = "http://icode.renren.com/getcode.do?t=web_login&rnd=Math.random()";
                result.CaptchaImage = LoginMgr.getInstance().getCaptchaImage(result.CaptchaUrl, ref cookies);
                result.ErrMessage = "需要输入验证码";
                return result;
            }
            if (retstr.IndexOf("您的用户名和密码不匹配") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirecturl = String.Format("http://x{0}.aoshi.renren.com/root/start.action", _acc.ServerId);

            processStartGame(redirecturl, result, ref cookies);
            return result;
        }
    }
}
